package org.rsbot.script.randoms;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Logger;
import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.Camera;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.Inventory;
import org.rsbot.script.methods.Mouse;
import org.rsbot.script.methods.NPCs;
import org.rsbot.script.methods.Objects;
import org.rsbot.script.methods.Settings;
import org.rsbot.script.methods.Walking;
import org.rsbot.script.wrappers.RSCharacter;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSItem;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSPath;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;

@ScriptManifest(authors={"Qauters"}, name="GraveDigger", version=1.6D)
public class GraveDigger extends Random
{
  private static final int[] coffinIDs = { 7587, 7588, 7589, 7590, 7591 };
  private static final int[] graveStoneIDs = { 12716, 12717, 12718, 12719, 12720 };
  private static final int[] filledGraveIDs = { 12721, 12722, 12723, 12724, 12725 };
  private static final int[] emptyGraveIDs = { 12726, 12727, 12728, 12729, 12730 };
  private static final int INTERFACE_READ_GRAVESTONE = 143;
  private static final int INTERFACE_READ_GRAVESTONE_MODEL = 2;
  private static final int INTERFACE_READ_GRAVESTONE_CLOSE = 3;
  private static final int INTERFACE_CHECK_COFFIN = 141;
  private static final int INTERFACE_CHECK_COFFIN_CLOSE = 12;
  private static final int[] INTERFACE_CHECK_COFFIN_ITEMS = { 3, 4, 5, 6, 7, 8, 9, 10, 11 };

  private static final int[] NOT_TO_DEPOSIT = { 1351, 1349, 1353, 1361, 1355, 1357, 1359, 4031, 6739, 13470, 14108, 1265, 1267, 1269, 1296, 1273, 1271, 1275, 15259, 303, 305, 307, 309, 311, 10129, 301, 13431, 313, 314, 2347, 995, 10006, 10031, 10008, 10012, 11260, 10150, 10010, 556, 558, 555, 557, 554, 559, 562, 560, 565, 8013, 4251, 8011, 8010, 8009, 8008, 8007 };

  private final ArrayList<Group> groups = new ArrayList();

  private int tmpID = -1; private int tmpStatus = -1;

  public GraveDigger() {
    this.groups.add(new Group(7614, new int[] { 7603, 7605, 7612 }));
    this.groups.add(new Group(7615, new int[] { 7600, 7601, 7604 }));
    this.groups.add(new Group(7616, new int[] { 7597, 7606, 7607 }));
    this.groups.add(new Group(7617, new int[] { 7602, 7609, 7610 }));
    this.groups.add(new Group(7618, new int[] { 7599, 7608, 7613 }));
  }

  public boolean activateCondition()
  {
    if (this.settings.getSetting(696) != 0) if (this.objects.getNearest(new int[] { 12731 }) != null) {
        this.tmpID = (this.tmpStatus = -1);
        return true;
      }
    return false;
  }

  public int loop()
  {
    if (this.npcs.getNearest(new String[] { "Leo" }) == null)
      return -1;
    Object localObject1;
    if (this.inventory.getCountExcept(coffinIDs) > 23) {
      if (this.interfaces.canContinue()) {
        this.interfaces.clickContinue();
        sleep(random(1500, 2000));
      }
      localObject1 = this.objects.getNearest(new int[] { 12731 });
      if (localObject1 != null) {
        if (!this.calc.tileOnScreen(((RSObject)localObject1).getLocation())) {
          this.walking.getPath(((RSObject)localObject1).getLocation()).traverse();
          this.camera.turnToObject((RSObject)localObject1);
        } else {
          ((RSObject)localObject1).doAction("Deposit");
        }
      }
      if (this.interfaces.get(11).isValid()) {
        sleep(random(700, 1200));
        this.interfaces.get(11).getComponent(17).getComponent(27).doAction("Dep");
        sleep(random(700, 1200));
        this.interfaces.get(11).getComponent(17).getComponent(26).doAction("Dep");
        sleep(random(700, 1200));
        this.interfaces.get(11).getComponent(17).getComponent(25).doAction("Dep");
        sleep(random(700, 1200));
        this.interfaces.get(11).getComponent(17).getComponent(24).doAction("Dep");
        sleep(random(700, 1200));
        this.interfaces.get(11).getComponent(17).getComponent(23).doAction("Dep");
        sleep(random(700, 1200));
        this.interfaces.getComponent(11, 15).doClick();
        return random(500, 700);
      }
      return random(2000, 3000);
    }

    if (!getMyPlayer().isMoving())
    {
      if (getMyPlayer().getAnimation() != 827)
      {
        if (this.interfaces.get(242).isValid())
        {
          if (this.interfaces.get(242).containsText("ready to leave")) {
            this.tmpStatus += 1;
          }
          this.interfaces.getComponent(242, 6).doClick();
        } else if (this.interfaces.get(64).isValid()) {
          this.interfaces.getComponent(64, 5).doClick();
        } else if (this.interfaces.get(241).isValid()) {
          this.interfaces.getComponent(241, 5).doClick();
        } else if (this.interfaces.get(243).isValid()) {
          this.interfaces.getComponent(243, 7).doClick();
        } else if (this.interfaces.get(220).isValid()) {
          this.interfaces.getComponent(220, 16).doClick();
        } else if (this.interfaces.get(236).isValid()) {
          if (this.interfaces.get(236).containsText("ready to leave"))
            this.interfaces.getComponent(236, 1).doClick();
          else
            this.interfaces.getComponent(236, 2).doClick();
        }
        else
        {
          Object localObject3;
          Object localObject4;
          if (this.interfaces.get(141).isValid()) {
            if (this.tmpID >= 0) {
              localObject1 = new int[INTERFACE_CHECK_COFFIN_ITEMS.length];

              localObject3 = this.interfaces.get(141);
              for (int m = 0; m < INTERFACE_CHECK_COFFIN_ITEMS.length; m++) {
                localObject1[m] = ((RSInterface)localObject3).getComponent(INTERFACE_CHECK_COFFIN_ITEMS[m]).getComponentID();
              }

              for (localObject4 = this.groups.iterator(); (((Iterator)localObject4).hasNext()) && (this.tmpID >= 0); ) {
                Group localGroup2 = (Group)((Iterator)localObject4).next();
                if (localGroup2.isGroup(localObject1)) {
                  localGroup2.coffinID = this.tmpID;
                  this.tmpID = -1;
                }
              }
            }
            atCloseInterface(141, 12);
          } else if (this.interfaces.get(143).isValid()) {
            int i = this.interfaces.get(143).getComponent(2).getComponentID();

            for (localObject3 = this.groups.iterator(); ((Iterator)localObject3).hasNext(); ) { localObject4 = (Group)((Iterator)localObject3).next();
              if (((Group)localObject4).isGroup(i)) {
                ((Group)localObject4).graveID = this.tmpID;
              }
            }
            atCloseInterface(143, 3);
          }
          else
          {
            Object localObject2;
            if ((this.tmpStatus == 0) && (this.tmpID != -1)) {
              for (localObject2 = this.groups.iterator(); ((Iterator)localObject2).hasNext(); ) { localObject3 = (Group)((Iterator)localObject2).next();
                if (((Group)localObject3).graveID == this.tmpID) {
                  localObject4 = this.objects.getNearest(new int[] { ((Group)localObject3).graveID });
                  if ((localObject4 == null) || (!setObjectInScreen((RSObject)localObject4))) {
                    this.log.info("Couldn't find grave, shutting down.");
                    this.game.logout(false);
                    return -1;
                  }

                  this.inventory.useItem(this.inventory.getItem(new int[] { coffinIDs[localObject3.coffinID] }), (RSObject)localObject4);

                  long l = System.currentTimeMillis();
                  while (System.currentTimeMillis() - l < 10000L) {
                    if (this.inventory.getItem(new int[] { coffinIDs[localObject3.coffinID] }) == null)
                    {
                      break;
                    }
                    sleep(random(400, 700));
                  }

                }

              }

              this.tmpID = -1;
            } else if ((this.tmpStatus == -1) && (this.objects.getNearest(filledGraveIDs) != null)) {
              localObject2 = this.objects.getNearest(filledGraveIDs);
              if ((localObject2 == null) || (!setObjectInScreen((RSObject)localObject2))) {
                this.log.severe("Couldn't find grave, shutting down.");
                this.game.logout(false);
                return -1;
              }
              ((RSObject)localObject2).doAction("Take-coffin");
            }
            else
            {
              int j;
              if ((this.tmpStatus == 0) && (this.objects.getNearest(emptyGraveIDs) != null)) {
                localObject2 = this.objects.getNearest(emptyGraveIDs);
                j = ((RSObject)localObject2).getID();
                for (int n = 0; n < emptyGraveIDs.length; n++)
                  if (emptyGraveIDs[n] == j) {
                    RSObject localRSObject = this.objects.getNearest(new int[] { graveStoneIDs[n] });
                    if ((localRSObject == null) || (!setObjectInScreen(localRSObject))) {
                      this.log.severe("Couldn't find grave stone, shutting down.");
                      this.game.logout(false);
                      return -1;
                    }

                    this.tmpID = ((RSObject)localObject2).getID();

                    localRSObject.doAction("Read");
                  }
              }
              else if (this.tmpStatus == -1) {
                localObject2 = new ArrayList();
                for (j = 0; j < coffinIDs.length; j++) {
                  ((ArrayList)localObject2).add(Integer.valueOf(j));
                }

                for (Group localGroup1 : this.groups) {
                  if (localGroup1.coffinID != -1) {
                    ((ArrayList)localObject2).remove(new Integer(localGroup1.coffinID));
                  }
                }

                if ((this.tmpStatus == -1) && (((ArrayList)localObject2).size() == 0)) {
                  this.tmpStatus += 1;
                }

                while (this.tmpStatus == -1) {
                  int k = random(0, ((ArrayList)localObject2).size());
                  if (this.inventory.getCount(new int[] { coffinIDs[((Integer)localObject2.get(k)).intValue()] }) > 0) {
                    this.tmpID = ((Integer)((ArrayList)localObject2).get(k)).intValue();
                    this.inventory.getItem(new int[] { coffinIDs[((Integer)localObject2.get(k)).intValue()] }).doAction("Check");

                    return random(1800, 2400);
                  }
                }
              } else if (this.tmpStatus == 0)
              {
                localObject2 = this.npcs.getNearest(new String[] { "Leo" });
                if ((localObject2 == null) || (!setCharacterInScreen((RSCharacter)localObject2))) {
                  this.log.severe("Couldn't find Leo, shutting down.");
                  this.game.logout(false);
                  return -1;
                }

                ((RSNPC)localObject2).doAction("Talk-to");
              }
            }
          }
        }
      }
    }
    return random(1400, 1800);
  }

  public boolean atCloseInterface(int paramInt1, int paramInt2) {
    RSComponent localRSComponent = this.interfaces.getComponent(paramInt1, paramInt2);
    if (!localRSComponent.isValid())
      return false;
    Rectangle localRectangle = localRSComponent.getArea();
    if ((localRectangle.x == -1) || (localRectangle.y == -1) || (localRectangle.width == -1) || (localRectangle.height == -1)) {
      return false;
    }
    int i = (int)(localRectangle.getWidth() - 4.0D) / 2;
    int j = (int)(localRectangle.getHeight() - 4.0D) / 2;
    int k = (int)(localRectangle.getMinX() + localRectangle.getWidth() / 2.0D);
    int m = (int)(localRectangle.getMinY() + localRectangle.getHeight() / 2.0D);
    this.mouse.click(k + random(-i, i) - 5, m + random(-j, j), true);
    return true;
  }

  public boolean setCharacterInScreen(RSCharacter paramRSCharacter)
  {
    for (int i = 0; i < 3; i++) {
      Point localPoint = paramRSCharacter.getScreenLocation();
      if (!this.calc.pointOnScreen(localPoint)) {
        switch (i) {
        case 0:
          this.camera.turnToCharacter(paramRSCharacter);

          sleep(random(200, 500));

          break;
        case 1:
          this.walking.walkTileMM(this.walking.getClosestTileOnMap(paramRSCharacter.getLocation().randomize(2, 2)));

          sleep(random(1800, 2000));
        default:
          while (getMyPlayer().isMoving()) {
            sleep(random(200, 500)); continue;

            return false;
          }
        }
      }
    }

    return true;
  }

  public boolean setObjectInScreen(RSObject paramRSObject)
  {
    for (int i = 0; i < 3; i++) {
      Point localPoint = this.calc.tileToScreen(paramRSObject.getLocation());
      if (!this.calc.pointOnScreen(localPoint)) {
        switch (i) {
        case 0:
          this.camera.turnToObject(paramRSObject);

          sleep(random(200, 500));

          break;
        case 1:
          this.walking.walkTileMM(this.walking.getClosestTileOnMap(paramRSObject.getLocation().randomize(2, 2)));
          sleep(random(1800, 2000));
        default:
          while (getMyPlayer().isMoving()) {
            sleep(random(200, 500)); continue;

            return false;
          }
        }
      }
    }

    return true;
  }

  class Group
  {
    int coffinID = -1;
    int graveID = -1;
    int graveStoneModelID;
    int[] coffinModelIDs;

    public Group(int paramArrayOfInt, int[] arg3)
    {
      this.graveStoneModelID = paramArrayOfInt;
      Object localObject;
      this.coffinModelIDs = localObject;
    }

    public boolean isGroup(int paramInt) {
      return this.graveStoneModelID == paramInt;
    }

    public boolean isGroup(int[] paramArrayOfInt) {
      for (int k : this.coffinModelIDs) {
        int m = 0;

        for (int i2 : paramArrayOfInt) {
          if (k == i2) {
            m = 1;
          }
        }

        if (m == 0) {
          return false;
        }
      }
      return true;
    }
  }
}